/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DTO;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author DatPLT
 */
public class UserAddSAXParser extends DefaultHandler{
    private User user = null;
    private boolean bUsername = false;
    private boolean bPassword = false;
    private boolean bFullname = false;
    private boolean bRole = false;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if(qName.equals("user")){
            user = new User();
        }
        if(qName.equals("username")){
            bUsername = true;
        }
        if(qName.equals("password")){
            bPassword = true;
        }
        if(qName.equals("fullName")){
            bFullname = true;
        }
        if(qName.equals("userRole")){
            bRole = true;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String str = new String(ch, start, length);
        str = str.trim();
        if(bUsername){
            user.setUsername(str);
            bUsername = false;
        }
        if(bPassword){
            user.setPassword(str);
            bPassword = false;
        }
        if(bFullname){
            user.setFullName(str);
            bFullname = false;
        }
        if(bRole){
            user.setUserRole(str);
            bRole = false;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if(qName.equals("user")){
            user.saveUser();
            user = null;
        }
    }

}
